package com.feiyuan.book.service.impl;

import com.feiyuan.book.entity.BookChapterEntity;
import com.feiyuan.book.service.BookChapterService;
import com.feiyuan.book.vo.BookShelfVO;
import com.feiyuan.common.utils.Constant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feiyuan.common.utils.PageUtils;
import com.feiyuan.common.utils.Query;

import com.feiyuan.book.dao.BookUserShelfDao;
import com.feiyuan.book.entity.BookUserShelfEntity;
import com.feiyuan.book.service.BookUserShelfService;
import org.springframework.util.CollectionUtils;


@Service("bookUserShelfService")
public class BookUserShelfServiceImpl extends ServiceImpl<BookUserShelfDao, BookUserShelfEntity> implements BookUserShelfService {

    @Autowired
    private BookChapterService bookChapterService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<BookUserShelfEntity> page = this.page(
                new Query<BookUserShelfEntity>().getPage(params),
                new QueryWrapper<BookUserShelfEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public Map<String, Object> backPress(Long userId, BookShelfVO bookShelf) {
        Map<String, Object> map = new HashMap<>();

        BookChapterEntity chapter = bookChapterService.getById(bookShelf.getChapterId());
        // 查询书架信息
        BookUserShelfEntity bookUserShelf = this.baseMapper.selectOne(new QueryWrapper<BookUserShelfEntity>().eq("user_id", userId).eq("book_id", chapter.getBookId()));
        bookUserShelf.setUpdateTime(new Date());
        bookUserShelf.setUserReadChapterId(bookShelf.getChapterId());
        bookUserShelf.setUserReadChapterIndex(bookShelf.getChapterIndex());
        this.baseMapper.updateById(bookUserShelf);

        return map;
    }

    @Override
    public Map<String, Object> bqgList(Long userId, Integer pageIndex, Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> params = new HashMap<>();
        params.put(Constant.PAGE, pageIndex.toString());
        params.put(Constant.LIMIT, pageSize.toString());
        List<BookShelfVO> shelfList = this.baseMapper.selectBqgList(new Query<BookShelfVO>().getPage(params), userId);
        if (!CollectionUtils.isEmpty(shelfList)) {
            for (BookShelfVO bookShelfVO : shelfList) {
                if (StringUtils.isEmpty(bookShelfVO.getChapterUrl())) {
                    BookChapterEntity chapter = bookChapterService.list(new QueryWrapper<BookChapterEntity>().eq("book_id", bookShelfVO.getBookId()).orderByAsc("chapter_web_id")).get(0);
                    bookShelfVO.setChapterUrl(chapter.getChapterWebHref());
                }
            }
        }
        map.put("shelfList", shelfList);

        return map;
    }

    @Override
    public Map<String, Object> deleteShelf(BookShelfVO bookShelfVO) {
        Map<String, Object> map = new HashMap<>();
        this.baseMapper.deleteById(bookShelfVO.getId());
        return map;
    }

}